package com.yuexun.beilunpatient.ui.my.ui;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.baselistener.ICallBackListener;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.contractmanage.ui.Act_ContractManageRecord;
import com.yuexun.beilunpatient.ui.family.ui.Act_Family_List;
import com.yuexun.beilunpatient.ui.main.bean.UpdateBean;
import com.yuexun.beilunpatient.ui.main.model.impl.UpdateModel;
import com.yuexun.beilunpatient.ui.main.presenter.IUpdatePresenter;
import com.yuexun.beilunpatient.ui.main.presenter.impl.UpdatePresenter;
import com.yuexun.beilunpatient.ui.main.ui.view.IUpdateView;
import com.yuexun.beilunpatient.ui.my.bean.MessageCountBean;
import com.yuexun.beilunpatient.ui.my.model.impl.MyModel;
import com.yuexun.beilunpatient.ui.my.presenter.IMyPresenter;
import com.yuexun.beilunpatient.ui.my.presenter.impl.MyPresenter;
import com.yuexun.beilunpatient.ui.my.ui.view.IMyView;
import com.yuexun.beilunpatient.ui.registration.ui.Act_My_Registration;
import com.yuexun.beilunpatient.utils.UpdateManager;
import com.yuexun.beilunpatient.widget.LoadingDialog;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/3/13 0013.
 */

public class Frag_My extends KJFragment implements IUpdateView, IMyView {
    @Bind(R.id.ic_message_count)
    TextView icMessageCount;
    @Bind(R.id.my_pic)
    ImageView myPic;
    @Bind(R.id.my_name)
    TextView myName;
    @Bind(R.id.my_id_tv)
    TextView myIdTv;
    @Bind(R.id.my_id)
    TextView myId;
    @Bind(R.id.my_version)
    TextView myVersion;
    @Bind(R.id.update_ic_arrow)
    ImageView updateIcArrow;

    IUpdatePresenter presenter;
    IMyPresenter presenter_my;
    LoadingDialog loadingDialog;
    Handler handler_blood;
    Timer time;
    TimerTask timetask;
    int countTime = 3;

    ICallBackListener listener;
    int allcount = 0;

    public void setBackListener(ICallBackListener listener) {
        this.listener = listener;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_my, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        presenter = new UpdatePresenter(new UpdateModel(), this);
        presenter_my = new MyPresenter(new MyModel(), this);
        try {
            myVersion.setText(getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        myName.setText(PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.USER_NAME));

        loadingDialog = new LoadingDialog(getActivity());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            allcount = 0;
            Map<String, String> params = new HashMap<>();
            params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
            params.put("targetId", "2");
            params.put("state", "0");
            presenter_my.getMessageCount(params);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        allcount = 0;
        Map<String, String> params = new HashMap<>();
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        params.put("targetId", "2");
        params.put("state", "0");
        presenter_my.getMessageCount(params);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.ic_message_icon, R.id.my_record_ll, R.id.my_info_ll, R.id.my_message_ll, R.id.my_login_password_ll, R.id.my_share_ll, R.id.my_feedback_ll,
            R.id.my_update_ll, R.id.my_law_ll, R.id.my_our_ll, R.id.my_clean_ll, R.id.my_card_ll, R.id.my_conceal_ll, R.id.my_order_ll,
            R.id.my_family_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.my_order_ll:
                startActivity(new Intent(getActivity(), Act_My_Registration.class));
                break;
            case R.id.my_record_ll:
                startActivity(new Intent(getActivity(), Act_ContractManageRecord.class));
                break;
            case R.id.ic_message_icon:
                startActivity(new Intent(getActivity(), Act_Message.class));
                break;
            case R.id.my_info_ll:
                startActivity(new Intent(getActivity(), Act_Personal.class));
                break;
            case R.id.my_card_ll:
                Intent intent_heal = new Intent(getActivity(), Act_HealthNo.class);
                startActivity(intent_heal);
                break;
            case R.id.my_message_ll:
                startActivity(new Intent(getActivity(), Act_Message.class));
                break;
            case R.id.my_login_password_ll:
                startActivity(new Intent(getActivity(), Act_Change_Psw.class));
                break;
            case R.id.my_share_ll:
                startActivity(new Intent(getActivity(), Act_Share.class));
                break;
            case R.id.my_feedback_ll:
                startActivity(new Intent(getActivity(), Act_Feedback.class));
                break;
            case R.id.my_update_ll:
                presenter.update();
                break;
            case R.id.my_family_ll:
                Intent intent_family = new Intent(getActivity(), Act_Family_List.class);
                intent_family.putExtra("type", "1");
                startActivity(intent_family);
                break;
            case R.id.my_law_ll:
                break;
            case R.id.my_our_ll:
                break;
            case R.id.my_conceal_ll:
//                Intent intent;
//                intent = new Intent(getActivity(), Act_Webview.class);
//                intent.putExtra("url", "https://51wangzhen.com/app/privacy.html");
//                startActivity(intent);
                Uri uri = Uri.parse("https://51wangzhen.com/app/privacy.html");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                break;
            case R.id.my_clean_ll:
                loadingDialog.show();
                handler_blood = new Handler() {
                    public void handleMessage(Message msg) {
                        if (msg.what > 0) {
                            Log.v("time", msg.what + "");
                        } else {
                            countTime = 3;
                            if (time != null) {
                                time.cancel();
                                time = null;
                            }

                            if (timetask != null) {
                                timetask.cancel();
                                timetask = null;
                            }
                            loadingDialog.dismiss();
                            Toast.makeText(getActivity(), "已清除缓存", Toast.LENGTH_SHORT).show();
                        }
                        super.handleMessage(msg);
                    }

                };

                time = new Timer(true);
                timetask = new TimerTask() {
                    public void run() {
                        if (countTime > 0) {
                            countTime--;
                        }
                        Message msg = new Message();
                        msg.what = countTime;
                        handler_blood.sendMessage(msg);
                    }

                };
                time.schedule(timetask, 0, 1000);
                break;
            default:
                break;
        }
    }

    @Override
    public void showUpdateInfo(UpdateBean updateBean) {
        try {
            int versionCode = getActivity().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0).versionCode;
            if (updateBean.getVersionCode() > versionCode) {
                UpdateManager updateManager = new UpdateManager(getActivity(), updateBean, versionCode);
                updateManager.showNoticeDialog();
            } else {
                Toast.makeText(getActivity(), "已经是最新版本", Toast.LENGTH_LONG).show();
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void fail() {

    }

    @Override
    public void showMessageCount(MessageCountBean messageCountBean) {
        if (messageCountBean.getStatus().equals("200")) {
            allcount = allcount + Integer.valueOf(messageCountBean.getDatas().get(0));
            if (listener != null) {
                Bundle bundle = new Bundle();
                bundle.putInt("count", allcount);
                listener.callback(bundle);
            }
            if (Integer.valueOf(messageCountBean.getDatas().get(0)) > 0) {
                icMessageCount.setVisibility(View.VISIBLE);
                icMessageCount.setText(messageCountBean.getDatas().get(0));
            } else {
                icMessageCount.setVisibility(View.GONE);
                icMessageCount.setText("");
            }
        }
    }
}
